//
//  OASelectPersonController.m
//  OASystem
//
//  Created by zhoupengfei on 15/5/3.
//  Copyright (c) 2015年 zpf. All rights reserved.
//

#import "OASelectPersonController.h"
#import "OADepartmentModel.h"
#import "HttpTools.h"
#import "MBProgressHUD.h"
#import "GDataDefines.h"
#import "Toast+UIView.h"
#import "OACheckBoxFootView.h"


@interface OASelectPersonController ()
@property(nonatomic,weak)OACheckBoxFootView * checkBoxFootView;
@property(nonatomic,strong)UIBarButtonItem * rightItem;
@end

@implementation OASelectPersonController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupRightItems];
//    OACheckBoxFootView * checkBoxFootView = [OACheckBoxFootView checkBoxFootView];
//    self.tableView.tableFooterView = checkBoxFootView;
//    self.checkBoxFootView = checkBoxFootView;
        _remind2 = @"0";
               UIView *view = [[UIView alloc]init];

    view.frame = CGRectMake(0, KScreenHeight-80, KScreenWidth,120);

            UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];

       button2.frame = CGRectMake(0, 40, KScreenWidth/2,40);
  button2.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    [button2 addTarget:self action:@selector(midaction2:) forControlEvents:UIControlEventTouchUpInside];

    [button2 setTitle:@"短信提醒" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    [button2 setImage:[UIImage imageNamed:@"shopping_xuanzhe_default"] forState:UIControlStateNormal];
        [button2 setImage:[UIImage imageNamed:@"shopping_xuanzhe_highlighted"] forState:UIControlStateSelected];
       [button2 layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft
                                    imageTitleSpace:20];
//         [view addSubview: button1];
[view addSubview: button2];
        UIView *view2 = [[UIView alloc]init];
    view2.frame = CGRectMake(0,0, KScreenWidth, 40);
    view2.backgroundColor  = [UIColor yellowColor];
_checkBoxFootView = [OACheckBoxFootView checkBoxFootView];
_checkBoxFootView.width = KScreenWidth;
view2 = _checkBoxFootView;
//[view2 addSubview:_checkBoxFootView];


[view addSubview: view2];
self.tableView.tableFooterView = view;
}
- (void)midaction2:(UIButton *)button{
            button.selected = !button.isSelected;
if (button.selected) {
    _remind2 = [NSString stringWithFormat:@"%d",1];
}else{
    _remind2 = @"0";


}

}
-(void)setupRightItems{
    UIBarButtonItem * operationItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(operationACtion)];
    self.navigationItem.rightBarButtonItem = operationItem;
    self.rightItem = operationItem;
}

#pragma mark 提交
-(void)operationACtion{
    
    NSMutableArray * tempArray = [NSMutableArray array];
    for (OADepartmentModel * model in self.personArray) {
        if (model.isSelected) {
            [tempArray addObject:model.department];
        }
    }
    
    if (tempArray.count == 0) {
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    self.rightItem.enabled = NO;
    NSString * selectPersons = [tempArray componentsJoinedByString:@","];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":self.fileId},@{@"sName":userId},@{@"segment":self.setpString},@{@"staff":selectPersons},@{@"ryorder":self.checkBoxFootView.orderString},@{@"famxml":self.body},@{@"type":self.remind2}];
    [HttpTools postWithURL:KexchdataURL arrays:array method:KSetsetsmsDeliVerMethod success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        self.rightItem.enabled = YES;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        GDataXMLElement *rootElement = [doc rootElement];
        NSArray *arrData= [rootElement elementsForName:@"info"];
        NSString * message= nil;
        for (GDataXMLElement * subXMLElement in arrData) {
            GDataXMLElement * element = [[subXMLElement elementsForName:@"msg"] objectAtIndex:0];
            message  = [element stringValue];
        }
    
            [self.view makeToast:@"发送成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOADetailData" object:nil];
            [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
    
        
    } failure:^(NSError *error) {
        self.rightItem.enabled = YES;
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        [self.view makeToast:@"发送失败"];
    }];

    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.personArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellIdentifier = @"cellIdentifier";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    OADepartmentModel * model = self.personArray[indexPath.row];
    cell.textLabel.text = model.department;
    if( self.personArray.count==1){
    
    model.isSelected = YES;
    }
    if (model.isSelected) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OADepartmentModel * model = self.personArray[indexPath.row];
    model.isSelected = !model.isSelected;
    [self.tableView reloadData];
}


@end
